package com.dyw.ycgovfinance.mvp.news.newsdetail;

import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.dyw.ycgovfinance.R;
import com.dyw.ycgovfinance.app.CustomApplication;
import com.dyw.ycgovfinance.bean.NewsDetailBean;
import com.dyw.ycgovfinance.commom.Config;
import com.dyw.ycgovfinance.commom.IntentKey;
import com.dyw.ycgovfinance.mvp.base.BaseActivity;
import com.dyw.ycgovfinance.util.CommonUtil;
import com.dyw.ycgovfinance.util.ToastUtil;
import com.dyw.ycgovfinance.util.schedulers.SchedulerProvider;

/**
 * Created by Administrator on 2017/6/20.
 */

public class ActivityNewsDetail extends BaseActivity implements NewsDetailContract.View{

    private WebView wv;
    private TextView tvNewsTitle;
    private TextView tvNewsTime;






    String mNewsId;

    NewsDetailContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        mPresenter = new NewsDetailPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
        initTitle(null,null,"新闻详情",true,null);
        initData();
        assignView();
        initView();
        getNewsDetail();
    }

    @Override
    public void initData() {
        mNewsId = getIntent().getStringExtra(IntentKey.INTENT_KEY_NEWS_ID);
    }

    @Override
    public void assignView() {
        wv = (WebView) findViewById(R.id.wv);
        tvNewsTitle = (TextView) findViewById(R.id.tv_news_title);
        tvNewsTime = (TextView) findViewById(R.id.tv_news_time);
    }

    @Override
    public void initView() {
        //CommonUtil.initWebView(wv);
        /*wv.getSettings().setJavaScriptEnabled(true);

        wv.setWebChromeClient(new WebChromeClient());*/


        CommonUtil.initWebView(wv);

        wv.requestFocus();



        wv.setWebViewClient(new WebViewClient(){


            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (!url.startsWith("http") & !url.startsWith("https")) {
                    return false;

                } else {

                    if (url.endsWith("doc") ||
                            url.endsWith("docx") ||
                            url.endsWith("xls") ||
                            url.endsWith("xlsx") ||
                            url.endsWith("ppt") ||
                            url.endsWith("pptx"))
                    {
                        /*Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(HttpServiceUtil.BASE_URL_FUNING_REAL+url));
                        startActivity(in);*/
                        view.loadUrl("https://view.officeapps.live.com/op/view.aspx?src="+url);
                    }else {
                        //view.loadUrl("https://view.officeapps.live.com/op/view.aspx?src="+HttpServiceUtil.BASE_URL_FUNING_REAL+url);
                        view.loadUrl(url);

                    }

                    return true;
                }



            }
        });
    }

    @Override
    public void getNewsDetail() {
        mPresenter.getNewsDetail(mNewsId);
    }

    @Override
    public void onGetNewsDetailSuccess(NewsDetailBean newsDetailBean ) {
        tvNewsTitle.setText(newsDetailBean.getTitle());
        tvNewsTime.setText("时间："+newsDetailBean.getSend_time());
        //wv.loadDataWithBaseURL(Config.URL_BASE_IN_USE,CommonUtil.makeHtmlImgFitScreen(newsDetailBean.getRemark()),"text/html","utf-8",null);
        wv.loadDataWithBaseURL(Config.URL_BASE_IN_USE,newsDetailBean.getRemark(),"text/html","utf-8",null);

    }

    @Override
    public void onGetNewsDetailError(String tip) {
        ToastUtil.showShort(this,tip);
    }



    @Override
    public void setPresenter(NewsDetailContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {
        showProgressDialog(false,null);
    }

    @Override
    public void dismissLoading() {
        dismissProgressDialog();
    }
}
